﻿using Corina.DataContext.Service;
using Corina.Datamodel.DataObject;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Corina.DataContext.Master
{
    public class AutoResolveMaster : MasterBase
    {
        public ResultInfo GetAllItems(string account, string password, int deviceKindId, string deviceSn, int index, int size)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                int count = 0;
                var table = GetInstance<AutoResolveService>().GetAllItems(ConnectionString(DatabaseKeys.CORINA_PROD), deviceKindId, deviceSn, index, size, ref count);
                string[] columns = table.Columns.OfType<DataColumn>().Select(t => t.ColumnName).ToArray();

                var items = (from t in table.AsEnumerable()
                             select new DataObjectInfo()
                             {
                                 Values = GetObjectOfValue(t, columns)
                             });

                result.DataObjects.AddRange(items);
                result.TotalCount = count;
            }

            return result;
        }

        public ResultInfo Insert(string account, string password, string deviceSn)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            

            if (resultValue == ResultValue.Succeed)
            {
                AutoResolveInfo entity = new AutoResolveInfo() { DeviceSn = deviceSn };

                resultValue = GetInstance<AutoResolveService>().Insert(ConnectionString(DatabaseKeys.CORINA_PROD), entity);

            }

            return new ResultInfo(resultValue);
        }

        public ResultInfo Update(string account, string password, int id, bool autoresolve)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);



            if (resultValue == ResultValue.Succeed)
            {

                bool flag = GetInstance<AutoResolveService>().Update(ConnectionString(DatabaseKeys.CORINA_PROD), id, autoresolve);

                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
            }

            return new ResultInfo(resultValue);
        }

        public ResultInfo Remove(string account, string password, int id)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            if (resultValue == ResultValue.Succeed)
            {

                bool flag = GetInstance<AutoResolveService>().Remove(ConnectionString(DatabaseKeys.CORINA_PROD), id);

                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
            }

            return new ResultInfo(resultValue);
        }
    }
}
